GBML manual / Backgrounds

background_create

Loads one project-owned background from a tileset and returns a handle to it - or `nothing` if the shared hardware budget is already full. It starts HIDDEN: nothing is on the screen until a Draw event calls draw_background with the handle. (x, y) is where the picture’s top-left corner starts, counted from the room’s top-left corner; every draw_background call then says where it goes for that frame. Past the picture’s edge the console repeats the picture; the picture’s "Repeat" setting (or the optional fourth argument, true or false) says whether this background does.

Works differently on Game Boy - The Game Boy has one background layer, which the room's picture uses, and the picture and its place have to be written right in the call, as numbers and a name.

Works differently on Game Boy Color - The Game Boy Color has one background layer, which the room's picture uses, and the picture and its place have to be written right in the call, as numbers and a name.

On other consoles this differs: see Side by side and How the consoles differ.

Syntax

background_create(x, y, item, repeat = true) -> background

Arguments

NameType
xreal
yreal
itemtileset
repeatbool (can be left out; then it is true)

Returns

background

Description

Creates one project-owned background painted from a tileset - hidden until a Draw event draws it with draw_background - and hands you back a reference to it, exactly the way instance_create hands you back a reference to a new instance. Check the result against nothing before using it: the game only has room for so many backgrounds to exist at once, and picture memory is limited too, so making one can fail. That is a normal, silent "nothing was created" outcome here, never a crash. Separately, a GBA can only ever show 4 backgrounds at once, room and project combined (2 in a room with a zoomable background), and a room with three tile layers and a shroud is already at that ceiling. A background that would be the one too many is not refused when it is made: it stays hidden in the frames that draw_background has no layer for it.

The background starts hidden. background_create loads the picture and claims its layer, but nothing is on the screen until a Draw event calls draw_background(bg, x, y) with the handle, in every frame it should be seen. A background not drawn in a frame is hidden in that frame. It keeps everything else about itself in the meantime: its layer (background_priority), its colors and its looks.

x and y are where the picture's top-left corner starts, as a place in the room counted from the room's top-left corner. draw_background says where it goes for each frame it is drawn.

A background you create is your own project's, not the current room's - it survives a room change (room_change) completely untouched, the same way the camera does. It keeps existing, and keeps costing its share of the memory, until you call background_destroy yourself.

Keep the value this call hands back, even if you have nothing to do with it right away - store it in an instance variable, the same habit the example below shows. It is your only way to ever refer to this background again: background_destroy, background_priority, and draw_background all need that exact handle, and there is no way to look one up after the fact. Call background_create and let the result fall on the floor unused (background_create(0, 0, ts_doc_dungeon); on its own, nothing capturing it) and the background still gets made - but it is never drawn, so it is never seen, and you have thrown away the only way to ever draw, move or destroy it for the rest of the project's run, and it will sit there permanently costing its share of the memory above. This is worth doing even when today's code never reads the variable again, the same way instance_create's own result is worth keeping - a natural first field to reach for the moment a later change wants to reposition or destroy what was created here.

Does it repeat past the picture's edge? The console repeats a background by default. The picture has a Repeat box in the Paint tab (under the canvas); untick it and every background made from that picture does not repeat. The optional fourth argument says it for one call and wins over the box: background_create(0, 0, ts_doc_dungeon, false) makes a background that does not repeat, whatever the picture's box says, and true makes one that does. See background_repeat and background_edge for what that means and what it costs. The box is read when the call names the picture itself; a picture held in a variable repeats unless you give the fourth argument.

A background drawn in a Draw event is in world space - it scrolls with the room, exactly like every instance and every room tile layer. Drawn in a Draw GUI event it is fixed to the screen instead.

Example

backdrop = background_create(0, 0, ts_doc_dungeon);
if (backdrop != nothing) {
    background_priority(backdrop, 2);
}

Makes one background from the dungeon tileset when this instance is created (it is hidden until a Draw event draws it), then checks it actually exists before giving it a priority - the habit worth keeping every time, since the current room's own tile layers and shroud may have already used up the shared budget.

On the GBA

A GBA can show at most 4 regular backgrounds at once, room- and project-owned COMBINED - and only 2 in a room that has a zoomable background (see zoom_background_position), where the console has fewer regular layers - a room with three tile layers and a shroud is already at that ceiling with zero headroom left, through no fault of anything this call does. Making one fails HONESTLY (returns `nothing`) when the console has no room for it to exist, rather than letting a project silently overflow and crash later, somewhere with no connection back to the call that actually caused it - always check the result against `nothing` before using it, the exact habit instance_create already establishes (`with (instance_create(...))`). Only a background that is being DRAWN uses one of the 4 showing-at-once layers, and it uses it only in the frames it is drawn: one that is made and not drawn this frame is hidden and costs none of them (it still costs its picture’s memory). If drawing one would put one layer too many on the screen, that draw is skipped and the background stays hidden that frame. The background keeps everything else about itself: its priority (background_priority), its palette (background_palette) and its looks stay until you destroy it. A background survives a room change (room_goto) completely unharmed: it is a project-owned resource, like the camera, not one of the room's own things that gets torn down - it keeps existing, keeps costing its share of the memory, until the project itself calls background_destroy. So a background made in an object's Create event needs that object's Cleanup event to call background_destroy on it: reloading the room (room_restart, room_change) throws the object away but not the background, and every reload would leave one more behind until the console's limit is used up and the new ones come back as `nothing`.

See also: background_destroybackground_countbackground_prioritydraw_backgroundbackground_repeatbackground_edgebackground_clipRoomsBudgets